﻿using UnityEngine;
using GameFramework;

namespace MYSTARFORCE
{
    /// <summary>
    /// 我的战机的逻辑
    /// </summary>
    public class MyAircraft : Aircraft
    {
        [SerializeField]
        private MyAircraftData m_MyAircraftData = null;

        /// <summary>
        /// 玩家战机可移动区域
        /// </summary>
        private Rect m_PlayerMoveBoundary = default(Rect);

        /// <summary>
        /// 目标坐标
        /// </summary>
        private Vector3 m_TargetPosition = Vector3.zero;

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);

            m_MyAircraftData = (MyAircraftData)userData;
            if(m_MyAircraftData == null)
            {
                Log.Error("MyAircraftData is invalid");
                return;
            }
            // 当实体显示的时候，把它的名字命名为“MyAircraft”
            if(m_MyAircraftData.Name != null)
            {
                Name = m_MyAircraftData.Name;

            }

            // 在场景中查找ScrollBg脚本
            ScrollBg scrollBg = FindObjectOfType<ScrollBg>();
            if(scrollBg == null)
            {
                Log.Warning("Can not find ScrollBg");
            }
            // 将PlayerMoveBoundary的值传递给m_PlayerMoveBoundary
            m_PlayerMoveBoundary = new Rect(
                scrollBg.PlayerMoveBoundary.bounds.min.x, //将X轴的最小值赋值给Rect.x
                scrollBg.PlayerMoveBoundary.bounds.min.z, //将Z轴的最小值赋值给Rect.y
                scrollBg.PlayerMoveBoundary.bounds.size.x, //将X轴的size赋值给Rect.width
                scrollBg.PlayerMoveBoundary.bounds.size.z//将Z轴的size赋值给Rect.height
                );

        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            // 当点击鼠标左键时
            // 关于GetMouseButton的解释:https://docs.unity.cn/cn/2019.4/ScriptReference/Input.GetMouseButton.html
            if (Input.GetMouseButton(0))
            {
                //将鼠标左键点击时鼠标的坐标转换为游戏中的世界坐标
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                //修改坐标
                m_TargetPosition = new Vector3(mousePos.x, 0f, mousePos.z);
                // 计算点击坐标和当前小飞机坐标，并返回一个Vector3
                Vector3 direction = m_TargetPosition - CachedTransform.localPosition;
                // TODO: 尝试发射子弹
                for (int i = 0; i < m_Weapons.Count; i++)
                {
                    Log.Info("try attack(),{0}",m_Weapons[i].Id);
                    m_Weapons[i].TryAttack();
                }

                // 如果direction的斜边长度小于等于0.00001(即1e-05)，则不进行计算
                if (direction.sqrMagnitude <= Vector3.kEpsilon)
                {
                    return;
                }
                

                // 计算每es时间的移动速度
                Vector3 speed = Vector3.ClampMagnitude(direction.normalized * m_MyAircraftData.Speed * elapseSeconds, direction.magnitude);

                // 设定新坐标，并约束其最小值和最大值
                float newX = Mathf.Clamp(CachedTransform.localPosition.x + speed.x, m_PlayerMoveBoundary.xMin, m_PlayerMoveBoundary.xMax);
                float newZ = Mathf.Clamp(CachedTransform.localPosition.z + speed.z, m_PlayerMoveBoundary.yMin, m_PlayerMoveBoundary.yMax);
                // 移动小飞机到新的坐标上

                CachedTransform.localPosition = new Vector3(newX,0,newZ);

            }
        }
    }
}